<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo  活动配置模型
 * @since 2015-7-13
 */
class ActivityConfigModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
        array('name', 'require', '配置名称必须选择', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('name', '1,20', '配置名称不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    	array('name', '/^[a-zA-Z]\w{0,39}$/', '标识不合法', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('name', '', '配置标识已经存在', self::VALUE_VALIDATE, 'unique'),
    	array('activity_id', 'integer', '活动必须选择', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('title', '1,40', '配置说明必须要填写', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    	array('type',array(0,1,2,3,4),'配置值的范围不正确！',self::MODEL_BOTH,'in'),
    );
  
    /* 自动完成规则 */
    protected $_auto = array(
        array('name', 'strtoupper', self::MODEL_BOTH, 'function'),
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', '1', self::MODEL_INSERT),
    	array('create_user_id', 'getUid', self::MODEL_INSERT, 'callback'),
    	
    );
    
    /**
     * @todo 返回主键
     * @see \Think\Model::getPk()
     * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
     */
    public function getPk(){
    	return 'activity_config_id';
    }
    
    public function test(){
    	return 1;
    }
    /**
     * @todo  获取活动下的配置信息
     * @param boolen is_create  true 已创建 false 未创建
     * @param int activity_id  活动id.
     * @author PHP@妖孽 <dino_ma@163.com>
     */
    public function getActivityConfig($activity_id){
    	//读取配置表,获取字段名和类型
    	return $this->field('activity_id,name,type,title,extra,value')->where(array('status'=>1,'activity_id'=>$activity_id))->order('`sort`  desc')->select();
    }
    
    /**
     * @todo 判断附属表是否存在
     * @param int activity_id 活动id
     * @author PHP@妖孽 <dino_ma@163.com>
     */
    public function checkTableExist($activity_id){
    	$db_name = C('DB_NAME');
    	$table_name =  C('DB_PREFIX').'activity_attached_'.$activity_id;//附属标名称
    	$sql = <<<sql
    	SELECT `TABLE_NAME` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$db_name}' AND TABLE_NAME = '{$table_name}' ;
sql;
    	//判断附属表是否存在
    	return $info = M()->query($sql);
    }
}
